package com.leedsoft.controller;

import com.leedsoft.common.AjaxReply;
import com.leedsoft.common.JSON;
import com.leedsoft.common.Page;
import com.leedsoft.controller.dto.AidPlanDto;
import com.leedsoft.controller.vo.AidPlanPageVo;
import com.leedsoft.domain.AidOrg;
import com.leedsoft.domain.AidPlan;
import com.leedsoft.domain.SysRscDict;
import com.leedsoft.service.AidOrgService;
import com.leedsoft.service.AidPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/aid_plan")
public class AidPlanController {
    @Autowired
    private AidPlanService aidPlanService;
    @Autowired
    private AidOrgService aidOrgService;

    @RequestMapping("/list")
    public ModelAndView list(ModelAndView modelAndView){
        List<SysRscDict> list = aidPlanService.getDictList();//获取数据字典信息
        List<AidOrg> aidOrgList = aidOrgService.queryList();//获取应急救援机构信息

        modelAndView.addObject("aidOrg",aidOrgList);
        modelAndView.addObject("dict",list);
        modelAndView.addObject("dict_json", JSON.gson.toJson(list));
        modelAndView.setViewName("aidPlan/aidPlan-list");
        return modelAndView;
    }

    @RequestMapping("/pageList")
    @ResponseBody
    public Map<String,Object> pageList(AidPlanPageVo planPageVo){
        Page<AidPlanDto> page = aidPlanService.getAidPlanPage(planPageVo);
        Map<String,Object> map = new HashMap<>();
        map.put("total",page.getCount());
        map.put("rows",page.getList());
        return map;
    }

    @RequestMapping("/edit")
    @ResponseBody
    public Map<String,Object> edit(AidPlan aidPlan){
        if(aidPlan.getId()==null){
            aidPlanService.add(aidPlan);
        }else {
            aidPlanService.update(aidPlan);
        }
        return AjaxReply.success();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(HttpServletRequest request){
        String id = request.getParameter("id");
        aidPlanService.delete(Long.parseLong(id));
        return AjaxReply.success();
    }

}
